package com.million.artservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.artservice.entity.Article;
import com.million.artservice.entity.ArticleBody;
import com.million.artservice.entity.vo.AddArticleVoInfo;
import com.million.artservice.entity.vo.ArticleVo;
import com.million.artservice.entity.vo.QueryArticleVo;
import com.million.artservice.mapper.ArticleMapper;
import com.million.artservice.service.ArticleBodyService;
import com.million.artservice.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.million.commonutils.R;
import com.million.servicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章 服务实现类
 * </p>
 *
 * @author 十麓
 * @since 2022-04-17
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleBodyService articleBodyService;

    //分页查询文章列表 返回Article 列表
    @Override
    public Map<String, Object> queryArticlePage (Page<Article> articlePage) {
        Page<Article> selectPage = baseMapper.selectPage(articlePage, null);
        Map<String, Object> map = new HashMap<>();
        map.put("records", selectPage.getRecords());
        map.put("current", selectPage.getCurrent());
        map.put("pages", selectPage.getPages());
        map.put("size", selectPage.getSize());
        map.put("total", selectPage.getTotal());
        map.put("hasNext", selectPage.hasNext());
        map.put("hasPrevious", selectPage.hasPrevious());
        return map;
    }

    //查询最热文章 前8条 写入到缓存中
    @Override
    @Cacheable(value = "article", key = "'queryEightHotArticle'")
    public List<Article> queryEightHotArticle () {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        //取前8条
        queryWrapper.orderByDesc("view_count").last("limit 8");
        return baseMapper.selectList(queryWrapper);
    }

    //根据文章id 获取文章详情 编写sql
    @Override
    public ArticleVo queryArticleById (String articleId) {
        return baseMapper.queryArticleById(articleId);
    }

    //================================================================

    //后台管理 文章管理 条件分页查询文章
    @Override
    public Page<ArticleVo> queryArticlePageConditions (Page<ArticleVo> articlePage, QueryArticleVo queryArticleVo) {
        QueryWrapper<ArticleVo> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.orderByDesc("gmtCreate");
        if (queryArticleVo == null) {
            return baseMapper.queryArticlePageConditions(articlePage, articleQueryWrapper);
        }
        String commentCounts = queryArticleVo.getCommentCounts();
        String viewCount = queryArticleVo.getViewCount();
        String begin = queryArticleVo.getBegin();
        String end = queryArticleVo.getEnd();
        if (!StringUtils.isEmpty(commentCounts)) {
            //大于这个评论数
            articleQueryWrapper.ge("commentCounts", commentCounts);
        }
        if (!StringUtils.isEmpty(viewCount)) {
            //大于这个观看数
            articleQueryWrapper.ge("viewCount", viewCount);
        }
        if (!StringUtils.isEmpty(begin)) {
            //大于
            articleQueryWrapper.ge("gmtCreate", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //小于
            articleQueryWrapper.le("gmtCreate", end);
        }
        return baseMapper.queryArticlePageConditions(articlePage, articleQueryWrapper);
    }

    //添加文章 返回文章Id
    @Override
    public String addArticleInfo (AddArticleVoInfo addArticleVoInfo) {
        Article article = new Article();
        BeanUtils.copyProperties(addArticleVoInfo, article);
        article.setArticleBodyId("111");
        article.setCommentCounts(0);
        int insert = baseMapper.insert(article);
        if (insert == 0) {
            throw new GuliException(20001, "添加课程失败");
        }
        article.setCommentCounts(0); //默认为0  这是我直接在表里添加的字段，没有设置默认为0，需要自己手动添加

        //得到文章Id
        String articleId = article.getId();
        //添加文章内容到文章内容表中
        ArticleBody articleBody = new ArticleBody();
        articleBody.setArticleId(articleId);
        articleBody.setContent(addArticleVoInfo.getArticleBody());
        articleBodyService.save(articleBody);
        article.setArticleBodyId(articleBody.getId());
        baseMapper.updateById(article);
        return articleId;
    }

    //根据文章Id 得到文章内容
    @Override
    public AddArticleVoInfo getArticleInfo (String articleId) {
        Article article = baseMapper.selectById(articleId);
        AddArticleVoInfo addArticleVoInfo = new AddArticleVoInfo();
        if (article != null) {
            BeanUtils.copyProperties(article, addArticleVoInfo);
        } else {
            throw new GuliException(20001, "查询文章信息失败");
        }
        //查询文章内容
        String articleBodyId = article.getArticleBodyId();
        ArticleBody articleBody = articleBodyService.getById(articleBodyId);
        addArticleVoInfo.setArticleBody(articleBody.getContent());
        return addArticleVoInfo;
    }

    //修改文章内容
    @Override
    public void updateArticleInfo (AddArticleVoInfo addArticleVoInfo) {
        Article article = new Article();
        BeanUtils.copyProperties(addArticleVoInfo, article);
        //修改文章
        int i = baseMapper.updateById(article);
        if (i == 0) {
            throw new GuliException(20001, "修改文章失败");
        }
        //修改文章内容
        //注意此时addArticleVoInfo 里面并没有文章内容id
        //可以通过addArticleVoInfo的文章id 查询
        String articleVoInfoId = addArticleVoInfo.getId();
        Article article1 = baseMapper.selectById(articleVoInfoId);
        String articleBodyId = article1.getArticleBodyId();
        ArticleBody articleBody = new ArticleBody();
        articleBody.setId(articleBodyId);
        articleBody.setContent(addArticleVoInfo.getArticleBody());
        articleBody.setArticleId(articleVoInfoId);
        //更新文章内容
        articleBodyService.updateById(articleBody);
    }

    //根据文章id 删除文章信息
    @Override
    public void deleteArticleInfo (String articleId) {
        //1.删除文章内容信息
        Article article = baseMapper.selectById(articleId);
        if (article == null) {
            throw new GuliException(20001, "查询不到文章");
        }
        String articleBodyId = article.getArticleBodyId();
        articleBodyService.removeById(articleBodyId);
        //2.删除文章信息
        baseMapper.deleteById(articleId);


    }
}
